home *** CD-ROM | disk | FTP | other *** search
/ ASME's Mechanical Engine…ing Toolkit 1997 December / ASME's Mechanical Engineering Toolkit 1997 December.iso / c_lang / pcc_code.lzh / CP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-03-24  |  2.5 KB  |  110 lines

  1. /* Copyright (c) 1985 Martin Nohr and Tom Serface
  2.  * All Rights Reserved
  3.  *
  4.  * Revision    Date        Description
  5.  * --------    ---------    --------------------------------------------
  6.  *
  7.  * Copies files to another directory or file
  8.  *
  9.  */
  10. #include <stdio.h>
  11. #include <fileFind.h>
  12.  
  13. #define EOS '\0'
  14. #define YES 1
  15. #define NO 0
  16.  
  17. main(argc,argv) 
  18. int argc;
  19. char *argv[];
  20. {
  21.     int mods, err, dir=NO;
  22.     char fspec[85],directory[85],outFile[85],*where;
  23.     char destination[85], dpath[85];
  24.     struct DataArea DTA;
  25.     
  26.     if(argc < 3)
  27.         usage();
  28.     strcpy(dpath,argv[argc-1]);
  29.  
  30. /* 
  31.  * Build a directory path for the copy to file
  32.  * It must be something like A: or A:\ or A:\xxx\yyy or A:\xxx\yyy\
  33.  */
  34.     if(dpath[strlen(dpath)-1] == '\\') /* Take the \ off the end of the line */
  35.         dpath[strlen(dpath)-1] = EOS;
  36.     if(dpath[strlen(dpath)-1] == ':') {
  37.         strcat(dpath,"\\");
  38.         dir=YES;
  39.     }
  40.     else if(GetMod(dpath,&mods) == 0) { /* Destination File exists */
  41.         if(mods & F_DIRECTORY) {
  42.             strcat(dpath,"\\");
  43.             dir=YES;
  44.         }
  45.         else {
  46.             printf("%s: Already Exists ...\n",dpath);
  47.             exit(2);
  48.         }
  49.     }
  50.     if(!dir && argc > 3)
  51.         usage();
  52.     argc -=2;
  53.     ++argv;
  54. /*
  55.  * Set up the Data Transfer Address Structure
  56.  */
  57.     SetDta(&DTA);
  58.     while(argc--) {
  59.         strcpy(fspec, *argv++);
  60.         strcpy(directory,fspec);
  61.         if((where=rindex(directory,'\\')) || (where=rindex(directory,':'))) {
  62.             *(where+1)=EOS;
  63.         }
  64.         else {
  65.             directory[0]=EOS;
  66.         }
  67.         if (findFirst(fspec,F_ALL)) {
  68.             do {
  69.                 err=YES;
  70.                 strcpy(outFile,directory);
  71.                 strcat(outFile,DTA.dta_name);
  72.                 strcpy(destination,dpath);
  73.                 GetMod(outFile,&mods);
  74.                 if(mods & F_DIRECTORY) {
  75.                     printf("%s: is a Directory ... Skipping\n",outFile);
  76.                     continue;    /* Skip Directories */
  77.                 }
  78.                 else if(!dir) {
  79.                     printf("Copying %s to %s ...\n",outFile, destination);
  80.                     err=copy(outFile,destination);
  81.                 }
  82.                 else {
  83.                     strcat(destination,DTA.dta_name);
  84.                     if(GetMod(destination,&mods) == 0) {
  85.                         if(mods & F_DIRECTORY)
  86.                             printf("%s: Is a Directory ... \n",destination);
  87.                         else
  88.                             printf("%s: Already Exists ...\n",destination);
  89.                     }
  90.                     else {
  91.                         err=copy(outFile,destination);
  92.                         printf("Copying %s to %s ...\n",outFile, destination);
  93.                     }
  94.                 }                                
  95.                 if(err == NO)
  96.                     printf("Couldn't Copy: %s to %s\n",outFile,destination);
  97.             } while (findNext(F_ALL));
  98.         }
  99.         else
  100.             printf("Can't find: %s\n",fspec);
  101.     }
  102. }
  103.  
  104. usage()
  105. {
  106.     printf("Usage: cp filespec filespec filespec directory\n");
  107.     printf("   or  cp file file\n");
  108.     exit(1);
  109. }
  110.